package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.anno.Logs;
import com.qk.domain.user.UserDO;
import com.qk.entity.User;
import com.qk.dto.user.UserDTO;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
@SuppressWarnings("all")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/dept/{deptId}")
    public Result getUsersByDeptId(@PathVariable("deptId") Integer deptId) {
        log.info("根据部门ID查询用户列表: {}", deptId);
        List<User> userList = userService.getUserListByDeptId(deptId);
        return Result.success(userList);
    }

    @GetMapping("/role/{roleLabel}")
    public Result getUsersByRoleLabel(@PathVariable("roleLabel") String roleLabel) {
        log.info("根据角色标签查询用户列表: {}", roleLabel);
        List<User> userList = userService.getUserListByRoleLabel(roleLabel);
        return Result.success(userList);
    }

    @GetMapping("/list")
    public Result getUsersList() {
        log.info("查询所有用户列表");
        List<User> userList = userService.getUserList();
        return Result.success(userList);
    }

    @GetMapping
    public Result getUsers(UserDTO userDto) {
        log.info("查询用户列表: {}", userDto);
        PageResult<UserDO> userPage = userService.getUsers(userDto);
        return Result.success(userPage);
    }

    @Logs("添加用户")
    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户: {}", user);
        userService.addUser(user);
        return Result.success();
    }

    @Logs("删除用户")
    @DeleteMapping("/{ids}")
    public Result deleteUsers(@PathVariable("ids") List<Integer> ids) {
        log.info("批量删除用户: {}", ids);
        userService.deleteUsers(ids);
        return Result.success();
    }

    @Logs("获取用户详情")
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        log.info("根据ID查询用户: {}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @Logs("更新用户")
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        log.info("更新用户: {}", user);
        userService.updateUser(user);
        return Result.success();
    }
}